package org.example.com;

import org.example.com.Excutor.DailyScheduler;
import org.example.com.annotation.LotSizeStrategy;
import org.example.com.entity.BillOfMaterials;
import org.example.com.entity.Inventory;
import org.example.com.entity.Item;
import org.example.com.entity.MasterProductionSchedule;
import org.example.com.monitor.InventoryMonitor;
import org.example.com.processor.MRPProcessor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class MRPTest {
    public static void main(String[] args) {
        // 创建物料
        Item frame = new Item("F001", "车架", 3, false, LotSizeStrategy.ECONOMIC_ORDER_QUANTITY, 10, 0);
        Item wheel = new Item("W001", "轮子", 4, false, LotSizeStrategy.ECONOMIC_ORDER_QUANTITY, 10, 0);
        Item tire = new Item("T001", "轮胎", 5, false, LotSizeStrategy.ECONOMIC_ORDER_QUANTITY, 10, 0);
        Item bike = new Item("B001", "自行车", 6, true, LotSizeStrategy.ECONOMIC_ORDER_QUANTITY, 10, 0);

        // 创建BOM
        BillOfMaterials bikeBOM = new BillOfMaterials();
        bikeBOM.setParent(bike);
        bikeBOM.addComponent(frame, 1);
        bikeBOM.addComponent(wheel, 2);

        BillOfMaterials wheelBOM = new BillOfMaterials();
        wheelBOM.setParent(wheel);
        wheelBOM.addComponent(tire, 1);

        List<BillOfMaterials> boms = Arrays.asList(bikeBOM, wheelBOM);

        // 设置库存
        Inventory inventory = new Inventory();
        inventory.addStock(frame, 5);
        inventory.addStock(tire, 10);
        inventory.addStock(bike, 3);

        // 设置主生产计划
        MasterProductionSchedule mps = new MasterProductionSchedule();
        mps.addRequirement(bike, 10000); // 需要生产10000辆自行车

        // 运行MRP
        MRPProcessor mrp = new MRPProcessor(boms, inventory);
        Map<Item, Integer> requirements = mrp.runMRP(mps);
        Map<Item, Integer> purchaseOrders = mrp.generatePurchaseOrders(requirements);

        System.out.println("\n净需求:");
        for (Map.Entry<Item, Integer> req : requirements.entrySet()) {
            if (req.getKey().isFinishedGood()){
                System.out.println("最终产品-"+req.getKey().getItemName() + ": " + req.getValue());
                System.out.println("最终产品库存-"+req.getKey().getItemName() + ": " + inventory.getCurrentStock(req.getKey()));
            }else {
                System.out.println(req.getKey().getItemName() + ": " + req.getValue());
            }
        }

        System.out.println("\n建议采购订单:");
        for (Map.Entry<Item, Integer> req : purchaseOrders.entrySet()) {
            if (req.getKey().isFinishedGood()){
                System.out.println("最终产品-"+req.getKey().getItemName() + ": " + req.getValue());
            }else {
                System.out.println(req.getKey().getItemName() + ": " + req.getValue());
            }
        }

        // 创建监控器
        ArrayList<Item> allItems = new ArrayList<>();
        allItems.add(frame);
        allItems.add(wheel);
        allItems.add(tire);
        allItems.add(bike);
        InventoryMonitor monitor = new InventoryMonitor(inventory, allItems);

        // 启动每日定时任务（每天早上8:00执行）
        DailyScheduler.scheduleDailyTask(monitor::dailyCheck, 8, 0, 0);

        System.out.println("✅ 系统已启动，每日库存检查将在每天 08:00 自动运行...");
    }
}